package com.birdy.room.service.impl;

import com.birdy.exception.ServiceException;
import com.birdy.room.mapper.RoomMapper;
import com.birdy.room.service.IRoomService;
import com.birdy.user.service.IUserService;
import com.birdy.vo.Room;
import com.birdy.vo.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.websocket.Session;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;

/**
 * @author birdy
 * @date 2022/4/11 9:02 AM
 */
@Service
public class RoomServiceImpl implements IRoomService {

    @Autowired
    private IUserService userService;
    @Autowired
    private RoomMapper roomMapper;


    @Override
    public List<UserInfo> getUserList(Map<String, Session> sessionMap) {
        ArrayList<UserInfo> res = new ArrayList<>();
        for(String openid : sessionMap.keySet()) {
            res.add(userService.getUserInfo(openid));
        }
        // 根据session对userInfos进行排序
        res.sort(Comparator.comparing(o -> sessionMap.get(o.getOpenId()).getId()));
        return res;
    }

    @Override
    public int addRoom(Room room) throws ServiceException {
        try {
            return roomMapper.addRoom(room);
        } catch (Exception e) {
            throw new ServiceException(e.getMessage());
        }
    }

    @Override
    public void deleteRoom(int roomId) {
        roomMapper.deleteRoom(roomId);
    }

    @Override
    public Object getRoomList() {
        return null;
    }

    @Override
    public Room getRoomInfo(int roomId) {
        return roomMapper.getRoomInfo(roomId);
    }
}
